package org.sjs.dreamfly.browser

import android.content.Context
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.webkit.WebViewClient
import android.widget.RelativeLayout
import org.sjs.dreamfly.R
import org.sjs.dreamfly.common.base.BaseFragment
import org.sjs.dreamfly.common.util.LogUtil
import org.sjs.dreamfly.widget.TitleBar

open class WebViewFragment : BaseFragment() {

    private lateinit var webView: PhemonWebView
    private var webViewMask: View? = null
    private lateinit var rootView: ViewGroup
    private var url: String? = null
    private var useTitleBar = true
    private var useWebViewMask = false

    fun getDefaultWebView(): PhemonWebView {
        return webView
    }

    open fun loadUrl(url: String,
                     newWebView: PhemonWebView? = null,
                     methed: Int = BrowserConst.InjectMethod.METHOD_REMOVE_ADS,
                     point: Int = BrowserConst.InjectPoint.POINT_ON_PROGRESS_MORE_THAN_1) {
        LogUtil.d(url)
        this.url = url
        (newWebView?:webView).inject(url, methods = methed, points = point)
    }

    fun useMacChromeUserAgent() {
        webView.useMacChromeUserAgent()
    }

    fun canGoBack(): Boolean {
        return webView.canGoBack()
    }

    fun goBack() {
        webView.goBack()
    }

    fun <T: WebViewClient> webViewClientAcceptVisitor(delegate: T) {
        webView.webViewClientAcceptVisitor(delegate)
    }

    fun useTitleBar(use: Boolean): Boolean {
        useTitleBar = use
        if (titleBar != null) {
            titleBar?.visibility = if (use) View.VISIBLE else View.GONE
            return true
        }
        return false
    }

    fun useWebViewMask(use: Boolean): Boolean {
        useWebViewMask = use
        if (webViewMask != null) {
            webViewMask?.visibility = if (use) View.VISIBLE else View.GONE
            return true
        }
        return false
    }

    @Deprecated("暂时不用")
    private fun createNewWebView(): PhemonWebView {
        val newWebView = PhemonWebView(requireContext())
        RelativeLayout.LayoutParams(
            ViewGroup.LayoutParams.MATCH_PARENT,
            ViewGroup.LayoutParams.MATCH_PARENT).apply {
            this.addRule(RelativeLayout.BELOW, R.id.title_bar)
            rootView.addView(newWebView, this)
        }
        return newWebView
    }

    fun releaseWebView(webView: PhemonWebView) {
        rootView.removeView(webView)
    }

    override fun onDestroy() {
        LogUtil.d("onDestroy")
        super.onDestroy()
        webView.stopLoading()
        webView.webChromeClient = null
    }

    override fun requireLayoutId(): Int {
        return R.layout.fragment_webview
    }

    override fun onBindViews() {
        val view = requireView()
        rootView = view.findViewById(R.id.root_view)
        webView = view.findViewById(R.id.web_view)
        webViewMask = view.findViewById(R.id.web_view_mask)
        titleBar = view.findViewById(R.id.title_bar)
        loadingView = view.findViewById(R.id.loading_view)
        if (requireCustomLayoutId() > 0) {
            LayoutInflater.from(context).inflate(requireCustomLayoutId(), view.findViewById(R.id.custom_container))
        }
    }

    override fun onInitViews() {
        titleBar?.setOnBackListener(object : TitleBar.OnActionListener {
            override fun onBackButtonClick() {
                onBackPressed()
            }
        })
        titleBar?.visibility = if (useTitleBar) View.VISIBLE else View.GONE
        webViewMask?.visibility = if (useWebViewMask) View.VISIBLE else View.GONE
    }

    override fun onLoadData() {
        url?.let {
            webView.loadUrl(it)
        }
    }

    override fun onBackPressed(): Boolean  {
        if (webView.canGoBack()) {
            webView.goBack()
            return true
        }
        return false
    }

    open fun requireCustomLayoutId(): Int {
        return 0
    }
}